package org.dandsoft.blackbox.snn;

import java.io.*;

/**
 * Created by IntelliJ IDEA.
 * User: dvilyuzhanin
 * Date: 25.03.2009 1:14:33
 */
public class EnviromentTraceReader {

    private DataInputStream in;
    private int min;
    private int max;

    public EnviromentTraceReader(File file) throws IOException {
        in = new DataInputStream(new FileInputStream(file));
        /* int magic = in.readInt();
        if (magic != EnviromentTraceWriter.FILE_MAGIC) {
            throw new IOException("Bad file magic: 0x" + Integer.toHexString(magic));
        }*/
        min = in.readInt();
        max = in.readInt();
    }

    public ObjectId readId() throws IOException {
        try {
            return new ObjectId(in.readInt());
        } catch (EOFException e) {
            return null;
        }
    }

    public int getMin() {
        return min;
    }

    public int getMax() {
        return max;
    }

    public Value readValue() throws IOException {
        try {
            return new Value(in.readFloat());
        } catch (EOFException e) {
            return null;
        }
    }


    public static void main(String[] args) throws Exception {
        File file = new File(args[0]);
        EnviromentTraceReader reader = new EnviromentTraceReader(file);
        ObjectId id = null;
        Value value = null;
        int size = reader.getMax() - reader.getMin();
        int counter = 0;
        while ((id = reader.readId()) != null) {
            Value val = reader.readValue();
            if (id.getValue() != Integer.MIN_VALUE) {
                System.out.print(id.getValue() + ":" + val.getValue() + ",");
            }
            counter++;
            if (counter >= size) {
                counter = 0;
                System.out.println("\n");
            }

        }
    }
}
